package com.hsy.bibc.modules.main;

import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.app.Fragment;
import android.view.KeyEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.RelativeLayout;

import com.deparse.dpframe.ui.activity.DPBaseActivity;
import com.deparse.dpframe.ui.fragment.DPBaseFragment;
import com.deparse.dpframe.ui.view.DPTabItemContainer;
import com.hsy.bibc.R;
import com.hsy.bibc.modules.main.actionbars.ActionBarMain;
import com.hsy.bibc.modules.main.actionbars.ActionBarMsg;
import com.hsy.bibc.modules.main.actionbars.ActionBarMy;
import com.hsy.bibc.modules.main.actionbars.IActionBar;
import com.hsy.bibc.modules.main.fragment.FragmentMain;
import com.hsy.bibc.modules.msg.fragment.FragmentMsg;
import com.hsy.bibc.modules.my.fragment.FragmentMy;

import java.lang.reflect.InvocationTargetException;

public class MainActivity extends DPBaseActivity implements View.OnClickListener {
    private static boolean isExit = false;
    protected RelativeLayout actionBarContainer;
    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            isExit = false;
        }
    };

    private void exit() {

        if (!isExit) {
            isExit = true;
            showToast(R.string.msg_exit);
            // 利用handler延迟发送更改状态信息
            handler.sendEmptyMessageDelayed(0, 2000);
        } else {
            finishAllActivities();
        }
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {

        if (keyCode == KeyEvent.KEYCODE_BACK) {
            exit();
            return false;
        }
        return super.onKeyDown(keyCode, event);

    }


    public DPTabItemContainer tab;
    private Class<?>[] fragments = {
            FragmentMain.class,
            FragmentMsg.class,
            FragmentMy.class
    };
    private Class<?>[] actionbars = {
            ActionBarMain.class,
            ActionBarMsg.class,
            ActionBarMy.class
    };

    private DPBaseFragment currentFragment;
    private View currentActionBar;

    private FrameLayout framentContainer;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        super.setContentView(R.layout.activity_main);
        getRootView().setBackgroundColor(getResources().getColor(R.color.app_background_color));

        actionBarContainer = (RelativeLayout) findViewById(R.id.action_bar_container);
        framentContainer = (FrameLayout) findViewById(R.id.container);


        tab = F(R.id.tabs);
        tab.setOnTabChangeListener(new DPTabItemContainer.OnTabChangeListener() {
            @Override
            public void beforeTabChange(int index, View tabWidget) {
                framentContainer.removeAllViewsInLayout();
            }

            @Override
            public void afterTabChange(int index, View tabWidget) {
                try {
                    View actionBar = (View) actionbars[index].getConstructor(Context.class).newInstance(MainActivity.this);
                    setActionBar(actionBar);
                    currentActionBar = actionBar;
                    if (actionBar instanceof IActionBar) {
                        ((IActionBar) actionBar).setActivity(MainActivity.this);
                    }

                    currentFragment = (DPBaseFragment) fragments[index].newInstance();
                    currentFragment.setAttchedActivity(MainActivity.this);
                    replaceFragment(R.id.container, currentFragment);
                } catch (InstantiationException e) {
                    e.printStackTrace();
                } catch (IllegalAccessException e) {
                    e.printStackTrace();
                } catch (NoSuchMethodException e) {
                    e.printStackTrace();
                } catch (InvocationTargetException e) {
                    e.printStackTrace();
                }
            }
        });
        tab.setCurrentTab(0);

    }

    private void setActionBar(View actionBar) {
        actionBarContainer.removeAllViewsInLayout();
        actionBarContainer.addView(actionBar, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
    }


    @Override
    protected void onResume() {
        super.onResume();

    }

    @Override
    public boolean dispatchKeyEvent(KeyEvent event) {
        return super.dispatchKeyEvent(event);
    }

    @Override
    protected void onStop() {
        super.onStop();
    }

    @Override
    public void onClick(View v) {
    }

    public DPBaseFragment getCurrentFragment() {
        return currentFragment;
    }

    public View getCurrentActionBar() {
        return currentActionBar;
    }

    public DPTabItemContainer getTab() {
        return tab;
    }

    public void replaceFragment(Fragment discovery) {
        replaceFragment(R.id.container, discovery);
    }
}
